// pages/setup-normal/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateShow: false,
    minDate: new Date(1990, 1, 1).getTime(),
    maxDate: new Date(2100, 10, 1).getTime(),
    currentDate: new Date().getTime(),
    MAXLENGTH: 500,
    num: 0,
    taskContent: '',
    meetingType: '',
    startTime: '',
    endTime: '',
    workTime: '',
    promise: '',
    response: '',
    participant: '',
    actionTitle: '',
    actionOptions: [],
    actionKey: ''
  },
  openSelect (e) {
    let defaultOptions = [
      { name: '姓名1' },
      { name: '姓名2' },
      { name: '姓名3' },
      { name: '姓名4' }
    ];
    const { key, title } = e.currentTarget.dataset;
    const mapOptions = {
      followType: [{name: '上门拜访'},{name: '维护跟进'}],
      followContent: [{name: '项目推荐'},{name:'催要款项'},{name:'日常维护'},{name:'合同签订'}]
    };
    if (mapOptions[key]) {
      defaultOptions = mapOptions[key];
    }
    this.setData({
      actionOptions: defaultOptions,
      actionTitle: title,
      actionKey: key,
      selectShow: true
    });
  },
  openSelectTime (e) {
    const { key } = e.currentTarget.dataset;
    this.setData({
      actionKey: key,
      dateShow: true
    });
  },
  onClose () {
    this.setData({ dateShow: false });
  },
  onConfirm (e) {
    const { actionKey } = this.data;
    this.setData({
      [actionKey]: new Date(e.detail).toLocaleDateString(),
      dateShow: false
    });
  },
  changeText (e) {
    this.setData({
      taskContent: e.detail.value,
      num: e.detail.value.length
    });
  },
  closeAction () {
    this.setData({ selectShow: false });
  },
  onSelect (e) {
    const { actionKey } = this.data;
    this.setData({
      [actionKey]: e.detail.name,
      selectShow: false
    });
  },
  toSelectPerson (e) {
    const { url } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `../reassignment/index?redirect=${url}`,
    });
  },
  validateForm (form) {
    const mapMessage = {
      taskContent: '请输入任务内容',
      meetingType: '请选择会议类型',
      startTime: '请选择开始时间',
      endTime: '请选择终止时间',
      workTime: '请输入预计工时',
      promise: '请输入自罚承诺',
      response: '请选择负责人'
    };
    for (let key in form) {
      if (!form[key]) {
        wx.showToast({
          title: mapMessage[key],
          icon: 'none'
        });
        return false;
      }
    }
    return true;
  },
  save () {
    const {
      taskContent,
      meetingType,
      startTime,
      endTime,
      workTime,
      promise,
      response,
      participant
    } = this.data;
    const params = {
      taskContent,
      meetingType,
      startTime,
      endTime,
      workTime,
      promise,
      response,
      participant
    };
    if (!this.validateForm(params)) return;
    // TODO:请求保存接口
    wx.navigateBack({
      delta: 1,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
